<jsp:include page="/templates/header.jsp"/>
<%@page import="database.Queries" %>
<%@page import="controller.Course" %>
<%@page import="java.util.LinkedList" %>
<%@page import="java.sql.SQLException" %>
<%@page import="java.sql.Connection" %>
<%@page import="database.Connect" %>
<%@page import="java.sql.SQLException"%>

<script type="text/javascript" src="/salonmorandsmigi/js/jquery.tablesorter.min.js"></script> 
<script type="text/javascript" src="/salonmorandsmigi/js/confirm.js"></script>

<script type="text/javascript">
$(document).ready(function() 
	    { 
	        $("#courses").tablesorter(); 
	    } 
); 
</script>

   

<%	
	Connection con=null;
	try{
		con=Connect.getConnection();
		con.setAutoCommit(false);
		String username = request.getUserPrincipal().getName();
		LinkedList<Course> courses = Queries.getUserCourses(con,username);
		con.commit();
		if (courses==null || courses.size()==0 ){ %> 
			<br/><br/>There are no  Available courses for registration
		<%}
		else{
			%>
			<div class="tablesorter" id="courses">
				<table border="3" width="30%">
				<thead>
				<tr>
					<th>Course name</th>
					<th>Credit</th>
					<th>Free Space</th>	
				</tr>
				</thead>
				<tbody>
			<%
		 	for (int i=0;i<courses.size();i++){
		 		String name = courses.get(i).getName();
		 		Float credit = courses.get(i).getPointNum();
		 		long free =courses.get(i).getCapacity()- Queries.studentNumInCourse(name);
		 		%>
		 			<tr align="center" id=<%=name%>>
		 			<td><a href="/salonmorandsmigi/course.jsp?name=<%=name%>"> <%=name%> </a></td>
		 			<td><%=credit%></td>
		 			<td><%=free%></td>
		 			</tr>
		 	<% }%>
		 		</tbody>
		 		</table>
		 	</div>
		 		<br/>
				<br/>
				
				<div class="sidebar">
				  find a course by name
			      <div class="gadget">
			        <div class="search">
			          <form method="get" id="search" action="/salonmorandsmigi/CourseServlet">
			            <span>
			            <input type="text" name="name" id="s" />
			            <input name="searchsubmit" type="image" src="/salonmorandsmigi/images/search.gif" value="Go" id="searchsubmit" class="btn"  />
			            <input type="hidden" name="action" value="search">
			            </span>
			          </form>
			          <div class="clr"></div>
			        </div>
			      </div>
			    </div>
		<%}
	}
	catch (SQLException e){
		con.rollback();
		  out.write("SERVER ERROR! PLEASE TRY AGAIN");
	}
	finally{
		if(con!=null) 
		{
			con.setAutoCommit(true);
		  	con.close();
		}
	} %>

<jsp:include page="/templates/sidebar.jsp"/>
<jsp:include page="/templates/footer.jsp"/>